﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using Infrastructure;
using SverigesRadio.SverigesRadioApp.Model;
using SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.Commands;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels
{
    public class ProgramCategoriesControlViewModel : ViewModelBase, IPivotViewModel
    {
        private SverigesRadioServiceCaller<ProgramCategories> categoryService;
        public const string TitleText_Programs = "Program A-Ö";
        public const string TitleText_ArchivedPrograms = "Arkiverade program";

        public ProgramCategoriesControlViewModel(MainViewModel parentViewModel)
        {
            ParentViewModel = parentViewModel;

            categoryService = new SverigesRadioServiceCaller<ProgramCategories>();
            categoryService.OnServiceCallReceived += CategoryService_OnServiceCallReceived;

            ParentViewModel.NavigationService.Navigated += NavigationService_Navigated;

            CategoriesVisibility = Visibility.Visible;
        }

        private void NavigationService_Navigated(object sender, NavigatedEventHandler e)
        {
            if (e.ViewType != ViewType.MainPage)
                return;

            SelectedProgramCategory = null;
        }

        public string TitleTextPrograms
        {
            get { return TitleText_Programs; }
        }

        public MainViewModel ParentViewModel { get; private set; }

        private ListItem<ProgramCategory> selectedProgramCategory;
        public ListItem<ProgramCategory> SelectedProgramCategory
        {
            get { return selectedProgramCategory; }
            set
            {
                ParentViewModel.ProgramsViewModel.Programs = null;
                ParentViewModel.ProgramsViewModel.CategoryTitleText = string.Empty;

                selectedProgramCategory = value;

                RaisePropertyChanged("SelectedProgramCategory");

                if (value == null)
                    return;

                if(value.ItemData != null)
                {
                    ParentViewModel.NavigationService.NavigateTo(ViewType.Programs, ProgramsViewModel.CategoryNameString + "=" + value.Title,
                        ProgramsViewModel.CategoryIdQueryString + "=" + value.ItemData.Id);
                }
                else
                {
                    ParentViewModel.NavigationService.NavigateTo(ViewType.Programs, ProgramsViewModel.CategoryNameString + "=" + value.Title);
                }
            }
        }


        private Visibility categoriesVisibility;
        public Visibility CategoriesVisibility
        {
            get { return categoriesVisibility; }
            set
            {
                categoriesVisibility = value;
                RaisePropertyChanged("CategoriesVisibility");
            }
        }

        private IList<ListItem<ProgramCategory>> programCategories;
        public IList<ListItem<ProgramCategory>> ProgramCategories
        {
            get { return programCategories; }
            set
            {
                programCategories = value;
                RaisePropertyChanged("ProgramCategories");
            }
        }

        private IList<Group<Program>> programs;
        public IList<Group<Program>> Programs
        {
            get { return programs; }
            set
            {
                programs = value;
                RaisePropertyChanged("Program");
            }
        }



        public void IndexChanged()
        {
            // Just do this call once
            if (ProgramCategories != null)
                return;

            ParentViewModel.IsBusy = true;
            categoryService.CallAsync(SverigesRadioServices.ProgramCategories);
        }



        void CategoryService_OnServiceCallReceived(object sender, SverigesRadioServiceCallerEventArgs<ProgramCategories> e)
        {
            ParentViewModel.IsBusy = false;

            List<ListItem<ProgramCategory>> listItems = e.ServiceResult.Categories.Select(p => new ListItem<ProgramCategory>(p.Title, p)).ToList();
            listItems.Insert(0, new ListItem<ProgramCategory>(TitleText_ArchivedPrograms, null));
            listItems.Insert(0, new ListItem<ProgramCategory>(TitleText_Programs, null));
            

            ProgramCategories = listItems;
        }

    }
}